<?php
class Core_Model_DbTable_TotalActions extends Zend_Db_Table_Abstract {
    protected $_name = 'total_actions';
    
    public function getUserTotalAction($id_sociaL_user, $start_time, $end_time) {
        $cols = array (
            'posts'     => 'SUM(posts)',
            'likes'     => 'SUM(likes)',
            'comments'  => 'SUM(comments)',
            'votes'     => 'SUM(votes)',
            'id_social_user'
        );
        $query = $this->select()
                      ->from($this->_name, $cols)
                      ->where("id_social_user = '$id_sociaL_user'")
                      ->where("time BETWEEN $start_time AND $end_time")
                      ->group("id_social_user");
        
        return $this->fetchRow($query); 
    }
    
    public function getTopFans( $id_social_page, 
                                $start_time,
                                $end_time,
                                $type   = 'total_point', 
                                $offset = 0, 
                                $count  = FAN_PER_PAGE ) {
        $cols = array (
            "id_social_user",
            "id_social_page",
            "user_name",
            "posts"         => "SUM(posts)",       
            "comments"      => "SUM(comments)",
            "likes"         => "SUM(likes)",
            "total_point"   => "SUM(total_point)"
        );
        $query = $this->select()
                      ->from($this->_name, $cols)
                      ->where("id_social_page = '$id_social_page'")
                      ->where("time BETWEEN $start_time AND $end_time")
                      ->group("id_social_user")
                      ->limit($count, $offset)
                      ->order("$type DESC");

        $query_result = $this->fetchAll($query);
        return $query_result->toArray();
    }
    
    public function insertAction($id_social_page, $id_social_user, $user_name, $action_type, $time, $action_point) {
        $time_of_day = Core_Util::getBeginDate($time);
        $data = array (
            'id_social_page' => $id_social_page,
            'id_social_user' => $id_social_user,
            'user_name'      => $user_name,
            'time'           => $time_of_day,
            'action'         => 1,
            'total_point'    => $action_point
        );
        $val  = $this->getAdapter()->quoteInto("(?)", $data);
        $sql  = "INSERT INTO {$this->_name} (`id_social_page`, `id_social_user`, `user_name`, `time` , `{$action_type}s`, `total_point`) VALUES ";
        $sql .= $val;
        $sql .= " ON DUPLICATE KEY UPDATE `{$action_type}s` = `{$action_type}s` + 1, total_point = total_point + $action_point ";
        $this->getAdapter()->query($sql);
    }
    
    public function insertActions($actions, $action_type) {
        $page_db = new Core_Model_DbTable_Pages();
        $translate_cols = $this->getTranslateCols($action_type);
        foreach ($actions as $action) {
            $id_social_page = $action[$translate_cols['id_social_page']];
            $id_social_user = $action[$translate_cols['id_social_user']];
            $time           = $action[$translate_cols['created_time']];
            $point          = $page_db->getPoint($id_social_page, $action_type);
            $user_name      = $action['from_name'];
            $this->insertAction($id_social_page, $id_social_user, $user_name, $action_type, $time, $point);
        }
    }
    
    protected function getTranslateCols($action_type) {
        if ($action_type == POST) {
            return array (
                'id_social_page' => 'to',
                'id_social_user' => 'from',
                'created_time'   => 'created_time'
            );
        } else {
            return array (
                'id_social_page' => 'id_social_page',
                'id_social_user' => 'id_social_user',
                'created_time'   => 'created_time'
            );
        }
    }
}
?>
